//
//  MYNewsShopDetailsCategoryCell.swift
//  MuYing
//
//  Created by tbb on 2020/3/6.
//  Copyright © 2020 TBB. All rights reserved.
//

import UIKit

class MYNewsShopDetailsCategoryCell: YQTableViewBaseCell, UICollectionViewDelegate,UICollectionViewDataSource{
    
    @objc var categorySelectedItemBlock:((_ :Int)->())?;
    
    @objc var standardList:[MYNewsShopDetailsStandardModel]? {
        didSet {
            self.categoryCollView.reloadData();
        }
    }
    
    
    lazy var titleLb: UILabel = {
        let titleLb = UILabel.factoryLable(font: UIFont.systemFont(ofSize: 11, weight: .medium), textColor: appSubTitleColor, text: kYQLSSwift("选择类型:"))
        return titleLb;
    }()
    private let itemSizeW:CGFloat = (kSwiftkScreenSizeWidth - 10 - 36 - 56) / 3.0;
    private let itemSizeH:CGFloat = 28.0;
    
    lazy var categoryCollView: UICollectionView = {
        let layout = UICollectionViewFlowLayout();
        layout.scrollDirection = .vertical;
        
        layout.itemSize = CGSize(width: itemSizeW, height: itemSizeH);
        layout.minimumLineSpacing = 8;
        layout.minimumInteritemSpacing = 18;
        let collView = UICollectionView(frame: CGRect(x: 10, y: 32, width: kSwiftkScreenSizeWidth-66, height: 85), collectionViewLayout: layout);
        collView.backgroundColor = UIColor.white;
        collView.delegate = self;
        collView.dataSource = self;
        collView.bounces = false;
        collView.showsVerticalScrollIndicator = false;
        collView.register(UINib.init(nibName: "MYNewsShopDetailsCategoryCollCell", bundle: nil), forCellWithReuseIdentifier: "MYNewsShopDetailsCategoryCollCell")
        return collView;
    }()
    
    override func layoutSubviews() {
        super.layoutSubviews();
        self.contentView.addSubview(titleLb)
        titleLb.mas_makeConstraints { (make) in
            make?.top.mas_equalTo()(12);
            make?.left.mas_equalTo()(10);
            make?.height.mas_equalTo()(16);
        };
        self.contentView.addSubview(self.categoryCollView)
        categoryCollView.mas_makeConstraints { (make) in
            make?.top.mas_equalTo()(32);
            make?.left.mas_equalTo()(10);
            make?.bottom.mas_equalTo()(-24);
            make?.right.mas_equalTo()(-56);
        }
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

extension MYNewsShopDetailsCategoryCell {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.standardList?.count ?? 0;
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MYNewsShopDetailsCategoryCollCell", for: indexPath) as? MYNewsShopDetailsCategoryCollCell else {
            return UICollectionViewCell();
        }
    
        var categoryModel:MYNewsShopDetailsStandardModel?
        guard let arr = self.standardList else {
            return UICollectionViewCell();
        }
        if indexPath.item < arr.count {
            categoryModel = arr[indexPath.item];
        }
        cell.standardModel = categoryModel;
        
        
        return cell;

    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let categorySelectedItemBlock = self.categorySelectedItemBlock {
            categorySelectedItemBlock(indexPath.item);
        }
        
    }
    
}
